package com.zzptc.shop.dao;

import com.zzptc.shop.db.IDB;
import com.zzptc.shop.db.MysqlDB;
import com.zzptc.shop.vo.Users;
import com.zzptc.shop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao implements IDAO {
    //登录
    public boolean login(VO vo) throws SQLException {
        //获取数据库链接
        IDB db = new MysqlDB();
        Connection conn = db.getConnection();
        //找一个工具
        String sql = "select * from t_easybuy_user where eu_user_name=? and eu_password=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        //给？注入值
        Users u = (Users) vo;
        ps.setString(1, u.getEu_user_name());
        ps.setString(2, u.getEu_password());
        //要工具完成查询工作
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return true;
        }
        return false;
    }

    @Override
    //注册
    public boolean insert(VO vo) throws SQLException {
        //获取数据库链接
        IDB db = new MysqlDB();
        Connection conn = db.getConnection();
        //用工具操作数据库
        PreparedStatement ps = conn.prepareStatement("insert into t_easybuy_user(eu_user_id,eu_user_name,eu_password)values (?,?,?)");
        Users users = (Users) vo;
        ps.setString(1, users.getEu_user_id());
        ps.setString(2, users.getEu_user_name());
        ps.setString(3, users.getEu_password());
        //操作数据库
        int i = ps.executeUpdate();
        //ps 释放 关闭链接
        ps.close();
        db.closeConn();
        return i > 0;
    }

    @Override
    public List getAll() throws SQLException {
        return null;
    }

    @Override
    public boolean deleteByID(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean update(VO vo) throws SQLException {
        return false;
    }

    @Override
    public VO getByID(VO vo) throws SQLException {
        return null;
    }

    public static void main(String[] args) {
        UserDao u = new UserDao();
        Users user = new Users();
        user.setEu_user_id("3");
        user.setEu_user_name("ccc");
        user.setEu_password("333");
        try {
            if (u.insert(user)) {
                System.out.println("succ");
            } else {
                System.out.println("error");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
